﻿//==============================================================================
// File: ValidationManager.cs
// Created: 2010-05-10
// Author: Piotr Włodek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with WpfRcp. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.Collections.Generic;

namespace WpfRcp.PropertyModel.Validation
{
    /// <summary>
    /// Represents a validation manager responsible for registering validation rules.
    /// </summary>
    public static class ValidationManager
    {
        private static readonly IDictionary<Type, RuleList> PerTypeRuleList;

        static ValidationManager()
        {
            PerTypeRuleList = new Dictionary<Type, RuleList>();
        }

        /// <summary>
        /// Checks if validation is enabled on a given <see cref="IPropertyContainer"/> container.
        /// </summary>
        /// <param name="container"></param>
        /// <returns></returns>
        public static bool IsValidationEnabled(IPropertyContainer container)
        {
            var propertyContainer = container as PropertyContainer;
            if (propertyContainer != null)
            {
                return propertyContainer.Setter == PropertyContainer.SetterStrategies.Standard;
            }

            return true;
        }

        #region Validation Rules For Type

        /// <summary>
        /// Adds new validation rule for a given type.
        /// </summary>
        /// <param name="type">Type for which <see cref="IValidationRule"/> is being added.</param>
        /// <param name="rule">Validation rule to be added.</param>
        public static void Register(Type type, IValidationRule rule)
        {
            if (type == null)
                throw new ArgumentNullException("type");

            if (rule == null)
                throw new ArgumentNullException("rule");

            GetRuleList(type).Add(rule);
        }

        /// <summary>
        /// Adds new validation callback for a given type.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="callback">Validation callbac to be added.</param>
        public static void Register(Type type, ValidateValueCallback callback)
        {
            if (type == null)
                throw new ArgumentNullException("type");

            if (callback == null)
                throw new ArgumentNullException("callback");

            Register(type, new DelegateValidationRule(callback));
        }

        internal static RuleList GetRuleList(Type type)
        {
            RuleList list;

            if (!(PerTypeRuleList.TryGetValue(type, out list)))
            {
                lock (PerTypeRuleList)
                {
                    if (!(PerTypeRuleList.TryGetValue(type, out list)))
                    {
                        list = new RuleList();
                        PerTypeRuleList.Add(type, list);
                    }
                }
            }

            return list;
        }

        #endregion

        #region Validation Rules For Property

        public static void Register(MetaProperty metaProperty, IValidationRule rule)
        {
            if (metaProperty == null)
                throw new ArgumentNullException("metaProperty");

            if (rule== null)
                throw new ArgumentNullException("rule");

            metaProperty.ValidationRules.Add(rule);
        }

        public static void Register(MetaProperty metaProperty, ValidateValueCallback validateValueCallback)
        {
            if (metaProperty == null)
                throw new ArgumentNullException("metaProperty");

            if (validateValueCallback == null)
                throw new ArgumentNullException("validateValueCallback");

            metaProperty.ValidationRules.Add(validateValueCallback);
        }

        #endregion

        #region Validation Rules For Property Container

        public static void Register(PropertyContainer propertyContainer, IObjectValidationRule rule)
        {
            if (propertyContainer == null)
                throw new ArgumentNullException("propertyContainer");

            if (rule == null)
                throw new ArgumentNullException("rule");

            propertyContainer.ObjectValidators.Add(rule);
        }

        public static void Register(PropertyContainer propertyContainer, ValidateObjectCallback validateObjectCallback)
        {
            if (propertyContainer == null)
                throw new ArgumentNullException("propertyContainer");

            if (validateObjectCallback == null)
                throw new ArgumentNullException("validateObjectCallback");

            propertyContainer.ObjectValidators.Add(validateObjectCallback);
        }

        #endregion
    }
}